package com.village.friend.redis;

import com.alibaba.fastjson.JSONObject;
import com.village.friend.entity.User;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

@Service
public class RedisService {
    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * 存储list并设置超时时间（分钟）
     *
     * @param listKey
     * @param list
     */
    public void setList2Redis(String listKey, List list, long expire) {
        redisTemplate.opsForList().leftPushAll(listKey, list);
        redisTemplate.expire(listKey, expire, TimeUnit.SECONDS);
    }

    /**
     * 获取list
     *
     * @param listKey
     * @return
     */
    public List<Object> getListFromRedis(String listKey) {
        return redisTemplate.opsForList().range(listKey, 0, -1);
    }


    /**
     * 存储对象并设置超时时间（分钟）
     *
     * @param key
     * @param o
     * @param expire
     */
    public void setObj(String key, Object o, long expire) {
        redisTemplate.opsForValue().set(key, o, expire, TimeUnit.SECONDS);
    }

    /**
     * 获取对象
     *
     * @param key
     * @return
     */
    public Object getObj(String key) {
        return redisTemplate.opsForValue().get(key);
    }


    /**
     * 存储数据
     */
    public void set(String key, String value) {
        ValueOperations<String, String> ops = stringRedisTemplate.opsForValue();
        ops.set(key, value);
    }

    public void set(String key, String value, long expire) {
        ValueOperations<String, String> ops = stringRedisTemplate.opsForValue();
        ops.set(key, value, expire, TimeUnit.SECONDS);
    }


    /**
     * 存储 map  数据
     */
    public void setMap(String key, Map<String, Object> map, long expire) {
        redisTemplate.opsForHash().putAll(key, map);
        redisTemplate.expire(key, expire, TimeUnit.SECONDS);
    }

    /**
     * 获取map 数据
     *
     * @param key
     * @return
     */
    public Map<String, Object> getMapFromRedis(String key) {
        return redisTemplate.opsForHash().entries(key);
    }

    public void setUser(String token, User user) {
        User u = new User();
        u.setUid(user.getUid());
        u.setName(user.getName());
        u.setToken(user.getToken());
        u.setLongitude(user.getLongitude());
        u.setLatitude(user.getLatitude());
        u.setGender(user.getGender());
        set(token, JSONObject.toJSONString(u), 10 * 24 * 3600);
    }

    public User getUser(String token) {
        return JSONObject.parseObject(get(token), User.class);
    }


    /**
     * 获取数据
     */
    public String get(String key) {
        ValueOperations<String, String> ops = stringRedisTemplate.opsForValue();
        return ops.get(key);
    }

    public void setValueByExpire(String key, String value, long expireSecond) {
        ValueOperations<String, String> ops = stringRedisTemplate.opsForValue();
        ops.set(key, value);
        stringRedisTemplate.expire(key, expireSecond, TimeUnit.SECONDS);
    }

    public int getInt(String key, int defaultValue) {
        String val = get(key);
        if (StringUtils.isNotEmpty(val)) {
            return Integer.parseInt(val);
        }
        return defaultValue;
    }

    public long getLong(String key, long defaultValue) {
        String val = get(key);
        if (StringUtils.isNotEmpty(val)) {
            return Long.parseLong(val);
        }
        return defaultValue;
    }


    /**
     * 设置超期时间
     */
    public boolean expire(String key, long expireSecond) {
        Boolean expire2 = stringRedisTemplate.expire(key, expireSecond, TimeUnit.SECONDS);
        return expire2;
    }


    /**
     * 删除数据
     */
    public void remove(String key) {
        stringRedisTemplate.delete(key);
    }


    /**
     * 自增操作
     *
     * @param delta 自增步长
     */
    public Long increment(String key, long delta) {
        return stringRedisTemplate.opsForValue().increment(key, delta);
    }


}
